package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.IWechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class WechatServiceImpl implements IWechatService {


    @Value("${wechat.appId}")
    private String appid;

    @Value("${wechat.appSecret}")
    private String secret;
  /*  @Override
    public String getOpenid(String code) {
        Map<String, Object>prams = new HashMap<>();
        prams.put("appid",appid);
        prams.put("secret",secret);
        prams.put("js_code",code);
       prams.put("grant_type","authorization_code");
        String url="https://api.weixin.qq.com/sns/jscode2session";

        String body = HttpRequest.get(url).form(prams).execute().body();
        System.out.println("body:"+body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode!=null&&errcode!=0){
            throw new RuntimeException("获取微信用户信息失败");
        }
        String openid = jsonObject.getString("openid");
        return openid;

    }*/

    @Override
    public String getOpenid(String code) {
        String Url="https://api.weixin.qq.com/sns/jscode2session";
        Map<String, Object> prams = new HashMap<>();
        //把请求参数添加到map中，千万不要写错
        prams.put("appid",appid);
        prams.put("secret",secret);
        prams.put("js_code",code);
        prams.put("grant_type","authorization_code");
        String body = HttpRequest.get(Url).form(prams).execute().body();
        //把返回的数据示例openid取出来
        JSONObject jsonObject = JSONObject.parseObject(body);
        System.out.println("body:"+body);
        Integer errcode = jsonObject.getInteger("errcode");
        //做一个判断
        if (errcode!=null&&errcode!=0){
            throw new BaseException("获取微信用户信息失败"+jsonObject.getString("errmsg"));
        }
        String openid = jsonObject.getString("openid");
        return openid;

    }


    /**
     * 获取手机号
     * @param detailCode
     * @return
     */

    @Override
    public String getPhone(String detailCode) {
        String accessToken = getAccessToken();
        String url="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+accessToken;

/*    向指定URL发送POST请求，传递一个包含detailCode的JSON字符串作为请求体，并将响应结果解析为JSON对象*/
        String body = HttpRequest.post(url).body("{\"code\":\"" + detailCode + "\"}").execute().body();
        JSONObject  jsonObject = JSONObject.parseObject(body);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode!=null&&errcode!=0) {
            throw new RuntimeException("获取手机号失败");
        }
        JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
        if(phoneInfo!=null){
           return phoneInfo.getString("phoneNumber");

        }else {
            throw new RuntimeException("获取手机号失败");
        }


    }

    /**
     * 获取 access_token
     * @return
     */
    private String getAccessToken() {
        String url="https://api.weixin.qq.com/cgi-bin/token";
        Map<String, Object>prams = new HashMap<>();
        prams.put("appid",appid);
        prams.put("secret",secret);
        prams.put("grant_type", "client_credential");
        String body = HttpRequest.get(url).form(prams).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        System.out.println(body);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode!=null&&errcode!=0){
            throw new RuntimeException("获取微信用户信息失败"+jsonObject.getString("errmsg"));

        }
        return jsonObject.getString("access_token");

    }
}
